<template>
	<!-- 待开始任务提交 -->
	<view class="container">
		<c-navBar :title="title" isPerch></c-navBar>

		<view class="content" v-if="title === '开始任务'">
			<view class="title">上传开始使用图片</view>
			<c-upLoadImgs :projectName="projectName" watermark :file.sync="file" :maxCount="6"></c-upLoadImgs>
		</view>

		<view v-else class="content">
			<view class="title">申请取消原因</view>
			<textarea class="textarea" v-model="cause" placeholder="请输入" />
		</view>

		<view class="btn-box">
			<view class="btn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				titles: ['开始任务', '申请取消'],
				title: '',
				file: [],
				cause: '',
				name: '',
				id: '',
				projectName: ''
			}
		},
		onLoad({
			type,
			name,
			id,
			projectName
		}) {
			this.title = this.titles[Number(type) - 1];
			this.name = name;
			this.id = id;
			this.projectName = projectName
		},
		methods: {
			submit() {
				if (this.title === '开始任务') {
					if (this.file.length === 0) {
						return uni.showToast({
							title: '请上传图片',
							icon: 'none'
						})
					} else {
						this.startTask()
					}
				} else {
					if (this.cause.length === 0) {
						return uni.showToast({
							title: '请输入取消原因',
							icon: 'none'
						})
					} else {
						this.cancel()
					}
				}
			},
			// 开始任务
			startTask() {
				if (this.name == 'worker') {
					this.$api.workerTaskUpdate({
						id: this.id,
						useStatus: '3',
						startTime: new Date(),
						startImage: this.file.toString(',')
					}).then(res => {
						if (res.code == 200) {
							console.log('任务开始')
							uni.navigateBack()
						}
					})
				} else {
					this.$api.machineTaskUpdate({
						id: this.id,
						useStatus: '3',
						startTime: new Date(),
						startImage: this.file.toString(',')
					}).then(res => {
						if (res.code == 200) {
							console.log('任务开始')
							uni.navigateBack()
						}
					})
				}

				console.log(12);
			},
			// 申请取消
			cancel() {
				if (this.name == 'worker') {
					this.$api.workerTaskUpdate({
						id: this.id,
						useStatus: '6',
						cancelCause: this.cause,
					}).then(res => {
						if (res.code == 200) {
							console.log('任务开始')
							uni.navigateBack()
						}
					})
				} else {
					this.$api.machineTaskUpdate({
						id: this.id,
						useStatus: '6',
						cancelCause: this.cause,
					}).then(res => {
						if (res.code == 200) {
							console.log('任务开始')
							uni.navigateBack()
						}
					})
				}
				console.log(13);
			}

		}
	}
</script>

<style scoped lang="scss">
	.container {
		.content {
			padding: 20rpx 32rpx;

			.title {
				font-weight: bold;
				font-size: 36rpx;
				color: #1A1A1A;
				margin-bottom: 24rpx;
			}

			.textarea {
				width: 94%;
				height: 200rpx;
				background: #FAFAFA;
				border-radius: 12rpx 12rpx 12rpx 12rpx;

				margin-bottom: 32rpx;
				padding: 24rpx;

			}
		}
	}
</style>